#include "KdTree2d.h"

#include <algorithm>

namespace Geometry
  {
  KdTree2dNode::KdTree2dNode(
    EBoundingDirection i_direction, double i_coord, const TPoint2d* ip_point,
    KdTree2dNode* ip_left, KdTree2dNode* ip_right)
    : m_direction(i_direction), m_coord(i_coord), mp_point(ip_point),
    mp_left(ip_left), mp_right(ip_right)
    {

    }

  double KdTree2dNode::GetCoord() const
    {
    return m_coord;
    }

  EBoundingDirection KdTree2dNode::GetDirection() const
    {
    return m_direction;
    }

  KdTree2d::KdTree2d(const std::vector<TPoint2d>& i_points)
    : mp_root(nullptr)
    {
    mp_root = _Construct(i_points, EBoundingDirection::BD_VERTICAL);
    }

  KdTree2dNode* KdTree2d::_Construct(const std::vector<TPoint2d>& i_points, EBoundingDirection i_direction)
    {
    size_t sz = i_points.size();
    
    if (sz == 0)
      return nullptr;

    std::vector<TPoint2d> points_1(sz);
    std::vector<TPoint2d> points_2(sz);
    for (size_t p = 0; p < sz; ++p)
      {
      points_1[p] = i_points[p];
      points_2[p] = i_points[p];
      }

    //std::sort(points_1.begin(), points_2()

    switch (i_direction)
      {
      case Geometry::BD_VERTICAL:
       
        if (sz == 1)
          {
          return new KdTree2dNode(EBoundingDirection::BD_HORIZONTAL, i_points[0].x, &i_points[0], nullptr, nullptr);
          }
          


        break;
      case Geometry::BD_HORIZONTAL:
        if (sz == 1)
          {
          return new KdTree2dNode(EBoundingDirection::BD_VERTICAL, i_points[0].y, &i_points[0], nullptr, nullptr);
          }


        break;
      default:
        break;
      }
      
    }

  }